package GameDemoDouDiZhu;

import java.util.*;

public class Room {
    //必须有一副牌
    private List<Card> allCards = new ArrayList<>();

    public Room() {
        //1.做出54张牌
        //点数：个数确定了，类型确定
        String [] numbers = {"3","4","5","6","7","8","9","J","Q","K","A","2"};
        //花色：个数确定了，类型确定
        String [] colors = {"♥","♠","♣","♦"};
        int size = 0;//表示每张牌的大小
        //遍历点数，再遍历花色
        for (String number : numbers) {
            //第1个牌是3
            size++;//1 2 3 ...
            for (String color : colors) {
                Card c = new Card(number,color,size);
                allCards.add(c);
            }
        }
        //单独存入大小王
        Card c1 = new Card("","🃏小王",++size);
        Card c2 = new Card("","🃏大王",++size);
        Collections.addAll(allCards,c1,c2);
        System.out.println("这是一副新牌："+allCards);

    }
    /**游戏启动方法*/
    public void start() {
        //1.洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后："+allCards);
        //2.发牌，先定义三个玩家
        List<Card>  zhangsan = new ArrayList<>();
        List<Card>  lisi = new ArrayList<>();
        List<Card>  wangwu = new ArrayList<>();
        //正式发牌给三个玩家，留下三张底牌
        for (int i = 0; i < allCards.size()-3; i++) {
            Card c = allCards.get(i);//拿牌
            //判断牌发给谁
            if (i%3==0) {
                zhangsan.add(c);
            }else if (i%3==1) {
                lisi.add(c);
            }else if (i%3==2) {
                wangwu.add(c);
            }
        }
        //对三个玩家的牌进行排序
        sortCards(zhangsan);
        sortCards(lisi);
        sortCards(wangwu);
        
        //看牌
        System.out.println("张三" + zhangsan);
        System.out.println("李四" + lisi);
        System.out.println("王五" + wangwu);
        List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());//51,52,3
        System.out.println("地主牌为"+lastThreeCards);
        lisi.addAll(lastThreeCards);
        System.out.println("李四抢到地主后的牌"+lisi);
    }



    /**
     * 集中进行排序
     * */

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize()-o1.getSize();
            }
        });
    }

}
